<template>
	<view>
		<!-- <view class="cu-bar">
			<view class="action">
				<text @click="clearClick">清空全部</text>
			</view>
			<view class="action">
				<text @click="BackClick">完成</text>
			</view>
		</view>
		 -->
		<checkbox-group class="block" @change="CheckboxChange">
			<view class="cu-form-group" v-for="(item,index) in DataList" :key="index">
				<view class="title"><text style="color: #fa6868;">{{item.name}}</text><text style="font-size:26rpx"> {{$t('book.time')+Seconds(item.time)}}</text></view>
				<checkbox class="red" :class="item.checked?'checked':''" :checked="item.checked?true:false" :value="item.id"></checkbox>
			</view>
		</checkbox-group>
		
		<view class="cu-tabbar-height margin-bottom"></view>
		
		<view class="padding-left padding-right zai-tabbar-bar" v-if="delArr.length===0">
			<!-- <button class="cu-btn block line-red margin-tb-sm lg" disabled>删除选中</button> -->
		</view>
		
		<view class="padding-left padding-right flex flex-direction zai-tabbar-bar" v-else>
			<button class="cu-btn bg-red margin-tb-sm lg" @click="DelClick">{{$t('book.del')}}({{delArr.length}})</button>
		</view>
		<view class="iconfont booklist" v-if="DataList.length===0 ">
			<text style="font-size:60rpx;">&#xe605;</text>
			<text>{{$t('book.all')}}</text>
		</view>
	</view>
</template>

<script>
	import _tool from '@/util/bookshelf.js';
	export default {
		data() {
			return {
				DataList: [], delArr: [],
			}
		},
		onLoad() {
			this.GetBookList();
		},
		methods: {
			GetBookList() {
				let that = this;
				
				that.DataList= uni.getStorageSync('bookshelf_time');
			},
			BackClick() {
				uni.navigateBack()
			},
			CheckboxChange (e) {
				let DataList = this.DataList, values = e.detail.value;
				this.delArr = values;
				for (let i = 0; i < DataList.length; ++i) {
					const item = DataList[i];
					if(values.includes(DataList.id)){
						this.$set(item,'checked',true)
					}else{
						this.$set(item,'checked',false)
					}
				}
			},
			Seconds(value) {
			    var theTime = parseInt(value);// 秒
			    var theTime1 = 0;// 分
			    var theTime2 = 0;// 小时
			    if (theTime > 60) {
			        theTime1 = parseInt(theTime / 60);
			        theTime = parseInt(theTime % 60);
			        if (theTime1 > 60) {
			            theTime2 = parseInt(theTime1 / 60);
			            theTime1 = parseInt(theTime1 % 60);
			        }
			    }
			    var result = "" + parseInt(theTime) + this.$t('user.m');
			    if (theTime1 > 0) {
			        result = "" + parseInt(theTime1) + this.$t('user.f') + result;
			    }
			    if (theTime2 > 0) {
			        result = "" + parseInt(theTime2) + this.$t('user.h')+ result;
			    }
			    return result;
			},
			DelClick() {
				let that = this;
				_tool.DelBookList(this.delArr).then(function(res) {
					that.GetBookList();
				});
			},
			clearClick() {
				let that = this;
				_tool.clearStorage().then(function(res) {
					that.GetBookList();
				});
			}
		}
	}
</script>

<style lang="less">
	
	.booklist {
		display: block;
		text-align: center;
		margin-top: 300rpx;
		color: #555555;
		font-size: 30rpx;
	} 
</style>
